﻿using System;
using System.Collections.Generic;
using System.Configuration;
using Citi.PTSConsolidation.Domain;
using Citi.PTSConsolidation.Domain.Utils;
using Citi.PTSConsolidation.SchedulerUI.Bases;
using Citi.RoutineBase;

namespace Citi.PTSConsolidation.SchedulerUI
{
    public class ManipulateEntrepotData
    {
        private static string PtsConnectionString { get { return ConfigurationManager.AppSettings["PtsConnectionString"]; } }
        
        public static void ExtractData(ICurrentAction currentAction, IDictionary<string, object> parameters, string tableName, int year, string robotUsername, string robotPassword, DateTime referenceDate)
        {
            currentAction.Description = "Data manipulation started";
            UpdateProjectAccount1Sum(currentAction, year);
            UpdateProjectAccount1SystemGocSum(currentAction, year);
            UpdateProjectClientGocSum(currentAction, year);
            UpdateSystemVarianceTable(currentAction, year, referenceDate);

            currentAction.Description = "Data manipulation Finished";
        }

        private static void UpdateSystemVarianceTable(ICurrentAction currentAction, int year, DateTime referenceDate)
        {
            currentAction.Description = "Updating System Variance Table";
            var query = BookOfWork.GetSaveSystemVarianceQuery(year, referenceDate);
            currentAction.Description = query;
            SqlHelper.ExecuteReader(query, PtsConnectionString);
        }

        private static void UpdateProjectClientGocSum(ICurrentAction currentAction, int year)
        {
            currentAction.Description = "Updating Project and Client GOC Sum";
            var query = BookOfWork.GetSumProjectClientGOCUpdateQuery(year, DateTime.Today);
            currentAction.Description = query;
            SqlHelper.ExecuteReader(query, PtsConnectionString);
        }

        private static void UpdateProjectAccount1SystemGocSum(ICurrentAction currentAction, int year)
        {
            currentAction.Description = "Updating Project, Account 1 and System GOC Sum";
            var query = BookOfWork.GetSumProjectAccount1SystemGOCUpdateQuery(year, DateTime.Today);
            currentAction.Description = query;
            SqlHelper.ExecuteReader(query, PtsConnectionString);
        }

        private static void UpdateProjectAccount1Sum(ICurrentAction currentAction, int year)
        {
            currentAction.Description = "Updating Project and Account 1 Sum";
            var query = BookOfWork.GetSumProjectAccount1UpdateQuery(year, DateTime.Today);
            currentAction.Description = query;
            SqlHelper.ExecuteReader(query, PtsConnectionString);
        }
    }

    public class ManipulateEntrepotDataCurrentYear : CurrentYear
    {
        protected override void ExecuteProcess(DateTime referenceDate, ref ICurrentAction currentAction, string networkUser, string networkPassword, Dictionary<string, object> parameters)
        {
            ManipulateEntrepotData.ExtractData(currentAction, parameters, EntrepotDatabaseNames.BookOfWork.TableNameCurrentYear, DateTime.Today.Year, 
                networkUser, networkPassword, referenceDate);
        }
    }

    public class ManipulateEntrepotDataNextYear : NextYear
    {
        protected override void ExecuteProcess(DateTime referenceDate, ref ICurrentAction currentAction, string networkUser, string networkPassword, Dictionary<string, object> parameters)
        {
            ManipulateEntrepotData.ExtractData(currentAction, parameters, EntrepotDatabaseNames.BookOfWork.TableNameNextYear, referenceDate.Year + 1, 
                networkUser, networkPassword, referenceDate);
        }
    }

    public class ManipulateEntrepotDataPreviousYear : PreviousYear
    {
        protected override void ExecuteProcess(DateTime referenceDate, ref ICurrentAction currentAction, string networkUser, string networkPassword, Dictionary<string, object> parameters)
        {
            ManipulateEntrepotData.ExtractData(currentAction, parameters, EntrepotDatabaseNames.BookOfWork.TableNamePreviousYear, DateTime.Today.Year - 1, 
                networkUser, networkPassword, referenceDate);
        }
    }
}
